package com.iemes.controller.production;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iemes.controller.index.BaseController;
import com.iemes.entity.DiscFormMap;
import com.iemes.mapper.Dispmapper;
import com.iemes.util.Common;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/disc/")
public class DispatchingController extends BaseController {

    @Inject
    private Dispmapper dispmapper;

    @RequestMapping("openUI")
    public String openUI(Model model){
        String [] btnArray = new String[]{"<button type=\"button\" id=\"addAccount\" class=\"btn btn-primary marR10\">新增</button>","<button type=\"button\" id=\"editAccount\" class=\"btn btn-info marR10\">编辑</button>","<button type=\"button\" id=\"delAccount\" class=\"btn btn-danger marR10\">删除</button>"};
        model.addAttribute("res", findByRes());
        model.addAttribute("btn",btnArray);
        return Common.BACKGROUND_PATH  + "/production/dispatching/dispatchingIndex";
    }
    //后台获取主要数据
    @ResponseBody
    @RequestMapping("getDiscPage")
    public PageInfo getHomeworkPage(){
        PageHelper.startPage(1, 10);
        //获取数据
        List<DiscFormMap> getDis = dispmapper.getAllData();
        PageInfo pageInfo = new PageInfo<>(getDis);
        return pageInfo;
    }
    //打开添加页面
    @RequestMapping("openAddUi")
    public String openAddUi(Model model){
        //查询作业编号
        model.addAttribute("homework",dispmapper.getHomeWorkCode());
        //查询生产批号
        model.addAttribute("work", dispmapper.getWorkCode());
        //查询生产批号相关数据
        return Common.BACKGROUND_PATH + "/production/dispatching/addDispatching";
    }
    //添加数据
    @ResponseBody
    @RequestMapping("addData")
    public String addData(){
        DiscFormMap discFormMap = getFormMap(DiscFormMap.class);
        try {
            dispmapper.addEntity(discFormMap);//添加数据
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "success";
    }
    //打开编辑页面
    @RequestMapping("openEdit")
    public String openEditUi(Model model, HttpServletRequest request){
        //根据id查询相关信息
        String id = request.getParameter("id");
        //查询作业编号
        model.addAttribute("homework",dispmapper.getHomeWorkCode());
        //查询生产批号
        model.addAttribute("work", dispmapper.getWorkCode());
        //查询指定内容
        model.addAttribute("data",dispmapper.getChoseData(id));

        return Common.BACKGROUND_PATH + "/production/dispatching/editDispatching";
    }
    //保存修改
    @ResponseBody
    @RequestMapping("saveData")
    public String saveData(){
        //获取数据
        DiscFormMap discFormMap = getFormMap(DiscFormMap.class);
        //根据id进行删除
        try {
            dispmapper.deleteByAttribute("id", discFormMap.get("id")+"", DiscFormMap.class);
            //然后在添加数据
            dispmapper.addEntity(discFormMap);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "success";
    }
    //删除方法
}
